package com.kolon.business.mCafeteriaMenu.controller;

import java.util.Map;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.kolon.framework.constant.Constant;
import com.kolon.framework.interfaces.SimpleController;
import com.kolon.framework.util.ComUtil;
import com.kolon.framework.util.DateUtil;
import com.kolon.framework.util.MsgUtil;

@Controller("com.kolon.business.mCafeteriaMenu.controller.MCafeteriaBoardModifyController")
public class MCafeteriaBoardModifyController implements SimpleController {

	@SuppressWarnings("rawtypes")
	@RequestMapping("/business/mCafeteriaMenu/mCafeteriaBoardModify.do")
	public ModelAndView controller(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		// 도메인
		String url = MsgUtil.getMessage("xml.url");
		// 세션
		HttpSession httpSession = request.getSession();
		Map userMap = (Map)httpSession.getAttribute(Constant.SESSION_USER_INFO);
		String id = (String)userMap.get("userId");
		String pw = (String)userMap.get("userPw");
		// 기본식당 쿠키 추출
		Cookie[] cookies = request.getCookies();
		String resCookie = "";
		for(int i=0 ; i<cookies.length ; i++) {
			Cookie cookie = cookies[i];
			if(cookie.getName().equals("resCookieVal")) {
				resCookie = cookie.getValue();
				break;
			}
		}
		// 캘린더 관련 다음/이전/현재일 계산
		String YMD = ComUtil.getNullCheck(request.getParameter("YMD"), DateUtil.getToday());
		String year = ComUtil.getNullCheck(YMD.substring(0, 4), DateUtil.getYearStr());
		String month = ComUtil.getNullCheck(YMD.substring(5, 7), DateUtil.getMonthStr());
		String day = ComUtil.getNullCheck(YMD.substring(8, 10), DateUtil.getDayStr());
		String CafeCD = "1";
		if(!"".equals(request.getParameter("res") == null ? "" : request.getParameter("res"))) {
			CafeCD = request.getParameter("res"); 
		}else if(!"".equals(resCookie)&& "".equals(request.getParameter("res") == null ? "" : request.getParameter("res"))) {
			CafeCD = resCookie;
		}else if("".equals(resCookie) && "".equals(request.getParameter("res") == null ? "" : request.getParameter("res"))) {
			CafeCD = "1";
		}
		String returnUrl = "/business/mCafeteriaMenu/mCafeteriaBoard.do?YMD="+YMD +"&res="+CafeCD; //등록후 Url		
		ModelAndView modelAndView = new ModelAndView();
		if(!"".equals(request.getParameter("inOpinion")) && request.getParameter("inOpinion")!=null ) {
			StringBuffer buffer = new StringBuffer();
			buffer.append(url);			
			buffer.append("/cafeteria_menu.aspx?Gubun=").append("RestOpinionEdit");
			buffer.append("&inCafeCD=").append(CafeCD);
			buffer.append("&inOpinionCD=").append(request.getParameter("OpinionCD"));
			buffer.append("&inEmoID=").append("1");
			buffer.append("&inYMD=").append(YMD);
			buffer.append("&inOpinion=").append(ComUtil.getEncode(request.getParameter("inOpinion"),"EUC-KR"));
			buffer.append("&userid=").append(id);
			// XML DOCUMENT 및 인증 처리
			ComUtil.getDocument(buffer.toString(), id, pw);
			modelAndView.addObject("scriptType", "009");
			modelAndView.addObject("message_url", returnUrl);
			modelAndView.setViewName("/business/common/alert");
		}
		modelAndView.addObject("YMD", YMD);
		modelAndView.addObject("OpinionCD", request.getParameter("OpinionCD"));
		modelAndView.addObject("msg", request.getParameter("msg"));
		modelAndView.addObject("res", CafeCD);
		return modelAndView;
	}

}